package uk.ac.aber.paws.client.idlelogger;

import java.io.IOException;

import uk.ac.aber.paws.client.idlelogger.gui.PAWSSysTray;
import uk.ac.aber.paws.client.net.SendMessage;
import uk.ac.aber.paws.client.updater.UpdateDetails;
import uk.ac.aber.paws.client.updater.UpdateManager;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * IdleLoggerUpdater.java
 * 
 * This thread initiates the update checks for all the specified files, as listed
 * in the run() routine.
 *    
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class IdleLoggerUpdater extends Thread {
	
	public void run() {
	
		// get update information relating to PAWS Client jar
		UpdateDetails updateDetails = getPawsClientDetails();
		boolean pawsClientUpdateStatus = UpdateManager.updateNow(updateDetails);
	
		// get update information relating to PAWS Client config
		updateDetails = getPawsClientConfigDetails();
		boolean pawsConfigUpdateStatus = UpdateManager.updateNow(updateDetails);
		
		// if the paws client jar or config file has been updated - requires a restart
		if (pawsClientUpdateStatus || pawsConfigUpdateStatus) {
			
			// send an instruction to the PAWS client agent to be restarted
			SendMessage.requestRestart(Integer.parseInt(IdleLogger.getMainConfig().getProperty("app.main.net.secureservercommsport")));
			
			// restart this IdleLogger to pick up the new JAR
			try {
				IdleLogger.restartApplication(null);
			} 
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		// get update information relating to PAWS Client Manager jar
		updateDetails = getPawsClientManagerDetails();
		UpdateManager.updateNow(updateDetails);
		
		// update the Wrapper JAR 
		updateDetails = getWrapperDetails();
		UpdateManager.updateNow(updateDetails);
		
		// update the Wrapper config
		updateDetails = getWrapperConfigDetails();
		UpdateManager.updateNow(updateDetails);
		
		// display the updates window
		PAWSSysTray.displayWindow("UPDATES");
				
	}
	
	/**
	 * Returns the source, intermediate and destination file information for the Wrapper jar
	 * @return Structure containing information
	 */
	public static UpdateDetails getWrapperDetails() {
		
		return new UpdateDetails(
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.libdir")+
				IdleLogger.getMainConfig().getProperty("app.main.wrappername"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.wrapppername"),
				
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.updatedir")+
				IdleLogger.getMainConfig().getProperty("app.main.wrapppername"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.wrapppername")+
				IdleLogger.getMainConfig().getProperty("app.main.checksumkey")
				);
	}
	
	/**
	 * Returns the source, intermediate and destination file information for the Wrapper config
	 * @return Structure containing information
	 */
	public static UpdateDetails getWrapperConfigDetails() {
		
		return new UpdateDetails(
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.libdir")+
				IdleLogger.getMainConfig().getProperty("app.main.wrappperconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.wrappperconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.updatedir")+
				IdleLogger.getMainConfig().getProperty("app.main.wrappperconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.wrappperconfigname")+
				IdleLogger.getMainConfig().getProperty("app.main.checksumkey")
				);
	}
	
	/**
	 * Returns the source, intermediate and destination file information for the PAWS Client Manager jar
	 * @return Structure containing information
	 */
	public static UpdateDetails getPawsClientManagerDetails() {
		
		return new UpdateDetails(
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.libdir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername"),
				
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.updatedir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientmanagername")+
				IdleLogger.getMainConfig().getProperty("app.main.checksumkey")
				);
	}
	
	/**
	 * Returns the source, intermediate and destination file information for the PAWS Client jar
	 * @return Structure containing information
	 */
	public static UpdateDetails getPawsClientDetails() {
		
		return new UpdateDetails(
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.libdir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientname"),
				
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.updatedir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsclientname")+
				IdleLogger.getMainConfig().getProperty("app.main.checksumkey")
				);
		
		
	}
	
	/**
	 * Returns the source, intermediate and destination file information for the PAWS Client config
	 * @return Structure containing information
	 */
	public static UpdateDetails getPawsClientConfigDetails() {
		
		return new UpdateDetails(
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.confdir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.main.homedir")+
				IdleLogger.getMainConfig().getProperty("app.main.updatedir")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsconfigname"),
				
				IdleLogger.getMainConfig().getProperty("app.update.updatelocation")+
				IdleLogger.getMainConfig().getProperty("app.main.pawsconfigname")+
				IdleLogger.getMainConfig().getProperty("app.main.checksumkey")
				);
	}
}

